package com.example.newspublish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.newspublish.pojo.entity.Comment;
import com.example.newspublish.pojo.form.CommentForm;
import com.example.newspublish.service.CommentService;
import com.example.newspublish.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/comment")
@Tag(name = "评论相关")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Operation(summary = "新增评论", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/addComment")
    public Result addComment(@RequestBody CommentForm commentForm) {
        log.debug("新增评论被调用");
        return commentService.addComment(commentForm);
    }

    @Operation(summary = "获取用户所有评论")
    @GetMapping("/commentList")
    public Result getCommentList() {

        return commentService.getCommentList();
    }

    @Operation(summary = "删除评论",security = {@SecurityRequirement(name = "Authorization")})//
    @DeleteMapping("/delete/{id}")
    public Result deleteCommentById(@PathVariable Long id) {
        commentService.deleteCommentById(id);

        return Result.success();
    }

    @Operation(summary = "分页获取评论")
    @GetMapping("/getListByPage")
    public Result getCommentsByPag (@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        Page<Comment> commentsByPage = commentService.getCommentsByPage(pageNum,pageSize);
        return Result.success("分页查询成功~",commentsByPage);
    }
}
